﻿using BLL;
using Common.Extend;
using Common.Msg;
using CTools.Controllers;
using System.Data;
using System.Web.Mvc;

namespace CTools.Areas.Datas.Controllers
{
    public class SQLController : CTools.Controllers.BaseController
    {

        // GET: Datas/SQL
        /// <summary>
        /// 数据库信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 数据库链接字符串
        /// </summary>
        /// <returns></returns>
        public ActionResult ConnStr()
        {
            return View();
        }
        /// <summary>
        /// 数据库对比
        /// </summary>
        /// <returns></returns>
        public ActionResult Compare()
        {
            return View();
        }
        public ActionResult TableInfo()
        {
            return View();
        }


        /// <summary>
        /// 获取数据库信息
        /// </summary>
        /// <param name="connStr"></param>
        /// <returns></returns>
        [JsonResultHandleError]
        public ActionResult GetSqlInfo(string connStr)
        {
            BLLSQLInfo bll = new BLLSQLInfo();
            DataTable dt = bll.GetInfo(connStr);

            doResult.Data = dt.ExToArrayList();
            doResult.EnumCode = EnumCode.Success;

            return Json(doResult);
        }
        [JsonResultHandleError]
        public ActionResult GetTable(string connStr)
        {
            BLLSQLInfo bll = new BLLSQLInfo();
            DataTable dt = bll.GetTable(connStr);

            doResult.Data = dt.ExToArrayList();
            doResult.EnumCode = EnumCode.Success;

            return Json(doResult);
        }
        [JsonResultHandleError]
        public ActionResult GetTableInfo(string connStr,int tableId)
        {
            BLLSQLInfo bll = new BLLSQLInfo();
            DataTable dt = bll.GetTableInfo(connStr, tableId);

            doResult.Data = dt.ExToArrayList();
            doResult.EnumCode = EnumCode.Success;

            return Json(doResult);
        }
        /// <summary>
        /// 获取数据库表比较结果
        /// </summary>
        /// <param name="connStrA"></param>
        /// <param name="connStrB"></param>
        /// <returns></returns>
        [JsonResultHandleError]
        public ActionResult CompareTable(string connStrA, string connStrB)
        {
            BLLSQLInfo bll = new BLLSQLInfo();
            DataTable dt1 = new DataTable();
            DataTable dt11 = new DataTable();
            if (string.IsNullOrEmpty(connStrA) == false)
            {
                dt1 = bll.GetColumns(connStrA);
                dt11 = dt1.DefaultView.ToTable(true, "TableName");
                dt1.Columns["TableName"].ColumnName = "TableNameA";
                dt1.Columns["ColumnName"].ColumnName = "ColumnNameA";
            }
            DataTable dt2 = new DataTable();
            DataTable dt21 = new DataTable();
            if (string.IsNullOrEmpty(connStrB) == false)
            {
                dt2 = bll.GetColumns(connStrB);
                dt21 = dt2.DefaultView.ToTable(true, "TableName");
                dt2.Columns["TableName"].ColumnName = "TableNameB";
                dt2.Columns["ColumnName"].ColumnName = "ColumnNameB";
            }
            DataTable dt = new DataTable();
            dt = dt1.UnionDataTable(dt2);

            dt11.PrimaryKey = new DataColumn[] { dt11.Columns["TableName"] };
            dt11.Merge(dt21);
            var data = new { list = dt.ExToArrayList(), listT = dt11.ExToArrayList() };

            doResult.Data = data;
            doResult.EnumCode = EnumCode.Success;
            return Json(doResult);
        }
    }
}